import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';

declare var form: any;
declare var map: any;
@Component({
  selector: 'book-local',
  templateUrl: './book.component.html',
  styleUrls: ['./book.component.scss']
})
export class BookLocationComponent implements OnInit {
  bfs: any = null;

  constructor(private msg: NzMessageService) {
    // 加载书签
    this.bfs = new form.FormSimpleService('cgisbook', {
      keyField: 'id',
      randomFields: ['id'],
      orderObject: { id: 0 },
      pageSize: 10,
    });
    this.bfs.loadData(true);
  }

  ngOnInit() {
  }


  save() {
    let bounds = { xmin: 0, ymin: 0, xmax: 0, ymax: 0 };
    bounds = form.ConfigService.map.getExtent();
    //必须把bouds转为json格式
    this.bfs.er.bookextent = JSON.stringify(bounds);
    this.bfs.addRow(this.bfs.er);
    this.bfs.save();
    this.bfs.er = {};
  }

  localByBook(item: any) {
    if (item.bookextent) {

      //解析JSON字符串
      const bookextent = JSON.parse(item.bookextent);
      const bounds: [number, number, number, number] = [bookextent.xmin, bookextent.ymin, bookextent.xmax, bookextent.ymax];
      form.ConfigService.map.zoomToExtent(bounds);
    } else {
      this.msg.error('该书签没有空间信息');
    }
  }
}
